/*
 * CaseRecycler.h
 *
 * A case recycler is used to control memory usage for a Case.
 *
 * Whenever a case is considered died, on evolutive algorithm, its memory
 * can be used by another case without calling memory allocation functions.
 *
 *  Created on: 29/05/2011
 *      Author: godinho
 */

#ifndef CASERECYCLER_H_
#define CASERECYCLER_H_

#include <list>
#include <map>
#include <controller/Case.h>
#include <pthread.h>


class CaseRecycler {
public:

	CaseRecycler();

	/**
	 *	Get a case from case's poll.
	 */
	Case* getCase();

	/**
	 * Make a case available
	 */
	void die(Case* c);

	static CaseRecycler* getInstance();

	static void releaseRecycler();

	virtual ~CaseRecycler();

private:

	std::list<Case*> mFreeCases;

	std::list<Case*> mUsed;

	static std::map<pthread_t, CaseRecycler*> mReciclerInstances;
};

#endif /* CASERECYCLER_H_ */
